//
//  AddContactsHeaderView.m
//  Findif
//
//  Created by Mac on 2019/2/18.
//  Copyright © 2019年 Cicel Chen. All rights reserved.
//

#import "AddContactsHeaderView.h"

@interface AddContactsHeaderView ()

@property (nonatomic, weak)UIView * backView;
@property (nonatomic, strong)UIButton * personButton;
@property (nonatomic, strong)UIButton * groupButton;
@property (nonatomic, strong)UIView * personLineView;
@property (nonatomic, strong)UIView * groupLineView;
@property (nonatomic, weak)UIView * lineView;
//@property (nonatomic, weak)UIButton * backButton;
//@property (nonatomic, weak)UIImageView * searchImageView;
//@property (nonatomic, weak)UILabel * searchLabel;
@property (nonatomic, weak)UITextField * textField;

@end

@implementation AddContactsHeaderView


- (instancetype)initWithFrame:(CGRect)frame{
    
    if ([super initWithFrame:frame]) {
        
        [self setupUI];
        
        //监听当键将要退出时
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillHide)
                                                     name:UIKeyboardWillHideNotification
                                                   object:nil];
    }
    
    return self;
}


- (void)setupUI{
    
    self.backgroundColor = ColorWithRGB(0xF4F5F6);
    self.userInteractionEnabled = YES;
    
    UIView * backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 45 * SCREENH6)];
    
    backView.backgroundColor = ColorWithRGB(0xf9f9f9);
    backView.userInteractionEnabled = YES;
    
    self.backView = backView;
    
    [self addSubview:self.backView];
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xe4e4e4);
    
    self.lineView = lineView;
    
    [self.backView addSubview:self.lineView];
    
    
    self.personButton = [self setupButtonWithTitle:@"找人"];
    self.personButton.selected = YES;
    [self.backView addSubview:self.personButton];
    self.groupButton = [self setupButtonWithTitle:@"找社团"];
    [self.backView addSubview:self.groupButton];
    
    self.personLineView = [self setupLineView];
    self.personLineView.hidden = NO;
    [self.backView addSubview:self.personLineView];
    self.groupLineView = [self setupLineView];
    [self.backView addSubview:self.groupLineView];
    
    
//    UIButton * backButton = [UIButton new];
//
//    backButton.backgroundColor = ZRWhiteColor;
//    backButton.layer.cornerRadius = 5 * SCREENH6;
//    backButton.layer.masksToBounds = YES;
//    backButton.layer.borderColor = [ColorWithRGB(0xe4e4e4) CGColor];
//    backButton.layer.borderWidth = 1.0f;
//    [backButton addTarget:self action:@selector(clickSearchButton) forControlEvents:UIControlEventTouchUpInside];
//
//    self.backButton= backButton;
//
//    [self addSubview:self.backButton];
//
//
//    UIImageView * searchImageView = [UIImageView new];
//
//    searchImageView.image = [UIImage imageNamed:@"搜索"];
//    searchImageView.userInteractionEnabled = NO;
//
//    self.searchImageView = searchImageView;
//
//    [self.backButton addSubview:self.searchImageView];
//
//
//    UILabel * searchLabel = [UILabel new];
//
//    searchLabel.text = @"搜索昵称";
//    searchLabel.textColor = ColorWithRGB(0x909090);
//    searchLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];
//    searchLabel.userInteractionEnabled = NO;
//
//    self.searchLabel = searchLabel;
//
//    [self.backButton addSubview:self.searchLabel];
    
    
    UITextField * textFiled = [[UITextField alloc] init];
    
    textFiled.borderStyle = UITextBorderStyleRoundedRect;
//    textFiled.backgroundColor = [UIColor clearColor];
//    textFiled.layer.borderColor = [[UIColor clearColor] CGColor];
    //设置光标颜色
    textFiled.tintColor = ZRBlackColor;
    //修改占位符文字颜色
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = ColorWithRGB(0xa5a5a5);
    attrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    textFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"搜索关键字" attributes:attrs];
    textFiled.textColor = ZRBlackColor;
    UIView * leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 35 * SCREENH6, 30)];
    //设置显示模式为永远显示(默认不显示)
    UIImageView * searchImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10 * SCREENH6, 6 * SCREENH6, 18 * SCREENH6, 18 * SCREENH6)];
    [leftView addSubview:searchImageView];
    textFiled.leftView = leftView;
    searchImageView.image = [UIImage imageNamed:@"搜索"];
    searchImageView.userInteractionEnabled = NO;
    textFiled.leftViewMode = UITextFieldViewModeAlways;
    textFiled.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    textFiled.returnKeyType = UIReturnKeyDone;
    
    self.textField = textFiled;
    [self addSubview:self.textField];
}

- (void)layoutSubviews{
    
    [self.personButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.backView);
        make.leading.mas_equalTo(self.backView).offset(110 * SCREENH6);
    }];
    
    [self.personLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.backView);
        make.height.mas_equalTo(1.5 * SCREENH6);
        make.leading.trailing.mas_equalTo(self.personButton);
    }];
    
    [self.groupButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.backView);
        make.trailing.mas_equalTo(self.backView).offset(-110 * SCREENH6);
    }];
    
    [self.groupLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.backView);
        make.height.mas_equalTo(1.5 * SCREENH6);
        make.leading.trailing.mas_equalTo(self.groupButton);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.mas_equalTo(self.backView);
        make.height.mas_equalTo(1 * SCREENH6);
    }];
    
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self).offset(15 * SCREENH6);
        make.trailing.mas_equalTo(self).offset(-15 * SCREENH6);
        make.top.mas_equalTo(self.lineView.mas_bottom).offset(10 * SCREENH6);
        make.height.mas_equalTo(30 * SCREENH6);

    }];
    
//    [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.leading.mas_equalTo(self).offset(15 * SCREENH6);
//        make.trailing.mas_equalTo(self).offset(-15 * SCREENH6);
//        make.top.mas_equalTo(self.lineView.mas_bottom).offset(10 * SCREENH6);
//        make.height.mas_equalTo(30 * SCREENH6);
//    }];
//
//    [self.searchImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.width.height.mas_equalTo(18 * SCREENH6);
//        make.leading.mas_equalTo(self.backButton).offset(10 * SCREENH6);
//        make.centerY.mas_equalTo(self.backButton);
//    }];
//
//    [self.searchLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.mas_equalTo(self.backButton);
//        make.leading.mas_equalTo(self.searchImageView.mas_trailing).offset(10 * SCREENH6);
//    }];
}


- (UIView *)setupLineView{
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xFFD733);
    lineView.hidden = YES;
    
    return lineView;
}


- (UIButton *)setupButtonWithTitle:(NSString *)title{
    
    UIButton * button = [UIButton new];
    
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:ZRBlackColor forState:UIControlStateSelected];
    [button setTitleColor:ColorWithRGB(0x909090) forState:UIControlStateNormal];
    [button addTarget:self action:@selector(cilckButton:) forControlEvents:UIControlEventTouchUpInside];
    button.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    return button;
}


- (void)cilckButton:(UIButton *)button{
    
    NSString * status;
    if(button == self.personButton){
        
        status = @"person";
        if (!button.selected) {
            
            button.selected = YES;
            
            self.personLineView.hidden = NO;
            self.groupButton.selected = NO;
            self.groupLineView.hidden = YES;
        }
    }else{
        status = @"group";
        if (!button.selected) {
        button.selected = YES;
        
        self.groupLineView.hidden = NO;
        self.personButton.selected = NO;
        self.personLineView.hidden = YES;
        }
    }
   
//    NSDictionary * dict = @{@"status":status};
//
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeAddType" object:nil userInfo:dict];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(changeAddTypeWithType:)])
    {
        
        [self.delegate changeAddTypeWithType:status];
    }
}

- (void)clickSearchButton{
    
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"clickAddSearchButton" object:nil];
    if (self.delegate && [self.delegate respondsToSelector:@selector(pushSearchViewController)])
    {
        
        [self.delegate pushSearchViewController];
    }
}


//实现UITextField代理方法
//- (BOOL)textFieldShouldReturn:(UITextField *)textField {
//
//    [textField resignFirstResponder];//取消第一响应者
//
//    if (textField.text.length > 0) {
//
//        if (self.delegate && [self.delegate respondsToSelector:@selector(getSearchString:)])
//        {
//
//            [self.delegate getSearchString:textField.text];
//        }
//    }
//
//    return YES;
//}

- (void)keyboardWillHide
{
    if (self.textField.text.length > 0) {
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(getSearchString:)])
        {
            
            [self.delegate getSearchString:self.textField.text];
        }
    }else{
  
        if (self.delegate && [self.delegate respondsToSelector:@selector(updateData)])
        {
            
            [self.delegate updateData];
        }
    }
}

@end
